
clear all
set more off
cd "..\..\Derived Data"

import delimited "acsleaid_05.csv"
qui compress
save "acs2005.dta", replace
clear all

foreach x of numlist 40 60{
insheet using "dfbartik2005_`x'km.csv", clear
qui compress

 
 drop sedaleaname v1 


*Merge in district-grade-year controls
merge m:1 leaid grade using "NCES_2005_to_merge.dta"
drop if _merge==2
drop _merge district_name state_name ell_2005_district spec_2005_district
*Change race #s to pct
foreach var of varlist male_2005 female_2005 asian_2005 hisp_2005 black_2005 white_2005{
gen pct_`var'=`var'/enroll_2005
drop `var'
}

*Merge in district-year controls
merge m:1 leaid using "NCES_2005_to_merge_district.dta"
drop if _merge==2
drop _merge district_name state_name district_name state_name

* merge acs
merge m:1 leaid using "acs2005.dta"
drop if _merge==2
drop _merge



foreach var of varlist studentteacher_2005 pct_ell_2005 pct_speced_2005 pct_FRPM_2005{
gen missing_`var'=(`var'==.)
su `var'
replace `var'=r(mean) if `var'==.
}



local fuel coal gas oil renewable

* if missing means no production
foreach m of local fuel{
 
 replace ayproduction2005`m'=0 if ayproduction2005`m'==.
replace annual`m'=0 if annual`m'==.
 replace annual`m'lag=0 if annual`m'lag==.

 *leave outs
 gen total`m'_leaveout`x'km=total`m'- annual`m'
 gen total`m'_leaveout`x'km_lag=total`m'_lag - annual`m'lag
 gen leaveout_growth`m'_`x'=(total`m'_leaveout`x'km-total`m'_leaveout`x'km_lag)/total`m'_leaveout`x'km_lag*100
 gen growth`m'=(total`m'-total`m'_lag)/total`m'_lag*100
 
 

}



gen totalprod_fuel`x'=ayproduction2005coal + ayproduction2005gas+ ayproduction2005oil+ ayproduction2005renewable


local fuel coal gas oil renewable

foreach m of local fuel {


display "`m'"
gen `m'share_`x'=ayproduction2005`m'/totalprod_fuel`x'


}


gen bartikleave_`x'=coalshare_`x'*leaveout_growthcoal_`x' + gasshare_`x'*leaveout_growthgas_`x' + oilshare_`x'*leaveout_growthoil_`x'+ renewableshare_`x'*leaveout_growthrenewable_`x'
gen bartik_`x'=coalshare_`x'*growthcoal + gasshare_`x'*growthgas + oilshare_`x'*growthoil+ renewableshare_`x'*leaveout_growthrenewable

gen bartikleavecoal_`x'=coalshare_`x'*leaveout_growthcoal_`x'  

gen bartikleaveoil_`x'=oilshare_`x'*leaveout_growthoil_`x'
gen bartikleavegas_`x'=gasshare_`x'*leaveout_growthgas_`x'
gen bartikleaverenew_`x'=renewableshare_`x'*leaveout_growthrenewable_`x'

drop if cs_mn_all_lag_cohort==.

outsheet using "dfbartik2005_`x'kmanalysis_annual.csv", comma replace


}
